import { JSEncrypt } from "jsencrypt/lib/JSEncrypt";

declare let CryptoJS: any;

export const encryptPwd = (password: string): string => {
  let pub_key = 'olvEsTYBHLQiWCsX';
  var p = CryptoJS.enc.Utf8.parse;
  var slRun_tl = CryptoJS.AES.encrypt;
  var cy = CryptoJS;
  var key = p(pub_key + pub_key);
  var iv = p(pub_key);
  var encrypt_pwd = slRun_tl(password, key,
    { keySize: 128 / 8, iv: iv, mode: cy.mode.CBC, padding: cy.pad.Pkcs7 }).toString();

  return encrypt_pwd
}

export const jsePwd = (publicKey: string, password: string) => {
  let jse = new JSEncrypt()
  jse.setPublicKey(publicKey)
  return jse.encrypt(password)
}

export const slRun_t = (info: string): any => {
  var bis = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCPpLfSzW7K7ZP1CGL6iUbOHXMtZaiRicIdiNzPDv29DdzSRRHzFhYZRa5FWI7mrIgcDha+eGHHfmoy3JPI0XwfHQ0w9mat6isasBp+ZTQhutkP2BaBGXZKYdVuOOV9TTVL45UTQAIzdJwSsBBDVCeo+xfyxt4gUxxtrdDa+iImWQIDAQAB';
  var encrypt = new JSEncrypt();
  encrypt.setPublicKey(bis);
  var encrypted = encrypt.encrypt(info);
  return encrypted;
}

export const getBase64 = (file: File): Promise<string | ArrayBuffer | null> =>
  new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });

export const dateFormat = (date: Date) => {
  let d;
  if (date) {
    d = new Date(date);
  } else {
    d = new Date();
  }
  return `${d.getFullYear()}-${_addZero(d.getMonth() + 1)}-${_addZero(d.getDate())}`;
}
const _addZero = (num: number): string => {
  if (num) {
    return num.toString().padStart(2, '0');
  } else {
    return '';
  }
}

export const downFile = (res: any, fileName: string) => {
  const url = URL.createObjectURL(res)
  let a = document.createElement('a')
  a.href = url
  a.download = fileName
  a.click()
}

export const getQueryString = (name: string) => {                                       //name为传入参数
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}

export const isPC = () => {
  const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
  if (flag) {
    return false
  } else {
    return true
  }
}
 
// 加密
export const aesReq = (data: any) => {
  const AES_KEY = 'cOdHFNHUNkZrjNaN' // 密钥, AES-128 需16个字符, AES-256 需要32个字符
  const AES_IV = 'oMChoRLZnTivcQyR' // 密钥偏移量，16个字符
 
  const key = CryptoJS.enc.Utf8.parse(AES_KEY)
  const iv = CryptoJS.enc.Utf8.parse(AES_IV)
  // console.log(JSON.stringify(data))
  const src = CryptoJS.enc.Utf8.parse(JSON.stringify(data))

  const encrypted = CryptoJS.AES.encrypt(src, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  })
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);  // Base64加密encode;
}
 
// 解密
export function decData(data: any) {
  const AES_KEY = '0123456789abcdef' // 密钥, AES-128 需16个字符, AES-256 需要32个字符
  const AES_IV = 'abcdef0123456789' // 密钥偏移量，16个字符

  const key = CryptoJS.enc.Utf8.parse(AES_KEY)
  const iv = CryptoJS.enc.Utf8.parse(AES_IV)

  const decrypted = CryptoJS.AES.decrypt(data, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.NoPadding
  })
  return CryptoJS.enc.Utf8.stringify(decrypted).toString()
}